var ports = require("../../utils/ports.js")
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navData: [{
      title: '全部',
      status: '',
      label: 'all'
    },
    {
      title: '待付款',
      status: 1,
      label: 'dfk'
    },
    {
      title: '待发货',
      status: 10,
      label: 'dfh'
    },
    {
      title: '待收货',
      status: 6,
      label: 'dsh'
    },
    {
      title: '待评价',
      status: 8,
      label: 'dpj'
    },
    {
      title: '售后',
      status: 9,
      label: 'sh'
    }
    ],
    currentIndex: 0,
    status: '',
    goodsInfoStatusList: []
  },
  onLoad: function (options) {
    var that = this;
    var status = options.status;
    that.setData({
      status: status
    })
    if (status == '') {
      that.setData({
        currentIndex: 0
      })
    }
    if (status == '1') {
      that.setData({
        currentIndex: 1
      })
    }
    if (status == '10') {
      that.setData({
        currentIndex: 2
      })
    }
    if (status == '6') {
      that.setData({
        currentIndex: 3
      })
    }
    if (status == '8') {
      that.setData({
        currentIndex: 4
      })
    }
    if (status == '9') {
      that.setData({
        currentIndex: 5
      })
    }
  },
  onShow: function (options) {
    this.getMemberOrderList()
  },
  //订单列表
  getMemberOrderList() {
    var that = this
    var req = {}
    req.ListName = 'MemberOrderList'
    req.type = 1
    if (that.data.status) {
      req.status = that.data.status
    }
    ports.ModuleMemberOrder.getMemberOrderList(req, that)
  },
  //选择订单状态
  changeStatusHandler(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var currentIndex = that.data.currentIndex
    if (index == currentIndex) {
      return
    }
    that.setData({
      MemberOrderList: [],
      MemberOrderListPage: [],
      status: this.data.navData[e.currentTarget.dataset.index].status,
      currentIndex: index
    })
    that.getMemberOrderList();
  },
  onPullDownRefresh: function () {
    var that = this;
    ports.ModuleAll.PullDownRefresh(that, () => {
      that.getMemberOrderList();
    })
  },
  onReachBottom: function () {
    var that = this;
    ports.ModuleAll.ReachBottom('MemberOrderList', that, res => {
      that.getMemberOrderList();
    })
  },
  //取消订单
  cancelMemberOrder(e) {
    var that = this
    var req = {}
    var id = e.currentTarget.dataset.id
    if (id) {
      req.memberOrderID = id
    } else {
      wx.showToast({
        title: '订单不存在',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '您真的要取消订单吗？',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '取消中',
            icon: 'none',
            mask: true
          })
          ports.ModuleMemberOrder.cancelMemberOrder(req, that, res => {
            if (res.data.header.code == 0) {
              wx.showToast({
                title: '取消成功',
                icon: 'none'
              })
              var MemberOrderList = that.data.MemberOrderList
              MemberOrderList.splice(e.currentTarget.dataset.index, 1)
              that.setData({
                MemberOrderList
              })
            } else {
              wx.showToast({
                title: res.data.header.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  //去付款
  payOrder(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    if (!id) {
      wx.showToast({
        title: '订单异常',
        icon: 'none'
      })
      return
    }
    var openid = wx.getStorageSync("USER_OPENID")
    if (!openid) {
      wx.showToast({
        title: '用户信息异常',
        icon: 'none'
      })
      return
    }
    wx.showToast({
      title: '提交中',
      icon: 'loading',
      mask: true
    })

    let payData = {}
    payData.memberOrderID = id
    payData.openid = openid
    payData.payWayID = app.paywayID
    payData.body = '购买商品'
    ports.ModuleMemberOrder.appApplyMemberOrderPay(payData, that, res => {
      if (res.data.header.code == 0) {
        var data = res.data.body;
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success(res) {
            wx.showToast({
              title: '支付成功',
              icon: 'none',
              mask: true,
              duration: 1000
            })
            var MemberOrderList = that.data.MemberOrderList
            MemberOrderList[e.currentTarget.dataset.index].status = 4
            that.setData({
              MemberOrderList
            })
          },
          fail() {
            wx.showToast({
              title: '取消支付',
              icon: 'none',
              mask: true,
              duration: 1000
            })
            setTimeout(() => {
              wx.redirectTo({
                url: '/pages/order/order',
              })
            }, 1000);
          }
        })
      } else {
        wx.showToast({
          title: res.data.header.msg ? res.data.header.msg : '系统异常',
          icon: 'none'
        })
      }
    })

  },
  //退款
  returnOrder(e) {
    var that = this
    var req = {}
    var id = e.currentTarget.dataset.id
    if (id) {
      req.memberOrderID = id
    } else {
      wx.showToast({
        title: '订单不存在',
        icon: 'none'
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '您真的要退货吗？',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '提交中',
            icon: 'none',
            mask: true
          })
          ports.ModuleMemberOrder.submitOneMemberOrderReturn(req, that, res => {
            if (res.data.header.code == 0) {
              wx.showToast({
                title: '提交成功',
                icon: 'none'
              })
              var MemberOrderList = that.data.MemberOrderList
              MemberOrderList[e.currentTarget.dataset.index].status = 14
              that.setData({
                MemberOrderList
              })
            } else {
              wx.showToast({
                title: res.data.header.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  //确认收货
  confirmMemberOrder(e) {
    var that = this
    var req = {}
    var id = e.currentTarget.dataset.id
    if (id) {
      req.memberOrderID = id
    } else {
      wx.showToast({
        title: '订单不存在',
        icon: 'none'
      })
      return
    }
    wx.showToast({
      title: '提交中',
      icon: 'none',
      mask: true
    })
    ports.ModuleMemberOrder.confirmMemberOrder(req, that, res => {
      if (res.data.header.code == 0) {
        wx.showToast({
          title: '收货成功',
          icon: 'none'
        })
        var MemberOrderList = that.data.MemberOrderList
        MemberOrderList[e.currentTarget.dataset.index].status = 8
        that.setData({
          MemberOrderList
        })
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },
  //去评价
  toEvaluated(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    var thisgoodsList = that.data.MemberOrderList[index].goodsList;
    if (thisgoodsList.length > 1) {
      wx.navigateTo({
        url: '/pages/orderdetail/orderdetail?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/evaluate/evaluate?id=' + thisgoodsList[0].id,
      })
    }
  },
  //去查看物流信息
  logistics(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/logistics/logistics?id=' + id
    })
  },
  //订单详情
  orderdetail(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/orderdetail/orderdetail?id=' + id
    })
  },
})